import {firebase} from "./core";

const github = new firebase.auth.GithubAuthProvider();
github.addScope('user:email');

/**
 * 注销
 * @returns {Promise<void>}
 */
export function logUserOut() {
  return firebase.auth().signOut();
}

/**
 * 使用 github 登陆
 * @returns {Promise<firebase.auth.UserCredential>}
 */
export function loginWithGithub() {
  return firebase.auth().signInWithPopup(github);
}

/**
 * 获取用户，如果有的话
 * @returns {Promise<unknown>}
 */
export function getFirebaseUser() {
  return new Promise(resolve => firebase.auth().onAuthStateChanged(user => resolve(user)))
}

/**
 * 获取令牌
 * @returns {Promise<unknown>|Promise<string>}
 */
export function getFirebaseToken() {
  const currentUser = firebase.auth().currentUser;
  if (!currentUser){
    return Promise.resolve(null);
  }
  return currentUser.getIdToken(true);
}
